package com.databias.j15;
/**
 * This class contains a screen object and a collection of drawable objects on the screen.
 * 
 * The class is instansiated with a screen, and then objects are added using the 'add' method.  
 * There is a corresponding 'remove' function to get rid of an object.
 * 
 * The draw function iterates through the list of objects and calls the corresponding draw
 * method for that object.  
 * 
 * @author Frederick
 *
 */

import java.util.Hashtable;
import java.util.ArrayList;
import java.awt.Color;
import java.awt.Shape;
import java.awt.Graphics2D;

public class GScreen {

	private int swidth, sheight;
	private Graphics2D screen;
	private ArrayList <Gsprite> sprites;
	
	/**
	 * This internal class stores the information needed to keep a collection of
	 * sprites.  
	 * 
	 * Does it need to store their coordinates?  If the object itself keeps them, no
	 * often, the g2d class doesn't care, it just draws what it is passed.  Should that
	 * paradigm be kept?  I just need the collection- otherwise, I need to make an interface
	 * for retrieving each one.  I think I'll just change it to a collection- don't need
	 * the GObject in that case since g2d already handles shapes among other things-
	 * and uses the same draw method.  But what about Text objects?   And images?  they're
	 * the other parts of a Graphics(2d) context.
	 * 
	 * @author Frederick Farzanegan
	 *
	 */
	private class Gsprite
	{
		int sx, sy;
		GObject sobject;
		
		Gsprite(int x, int y, GObject go)
		{
			sx=x;
			sy=y;
			sobject=go;
		}
		
		void draw()
		{
			sobject.draw(sx,sy,screen);
		}
	}
	

	
	/**
	 * Create a screen object class by giving a Graphics screen
	 * @param s
	 */
	public GScreen(Graphics2D s)
	{
		
		screen=s;
		Shape sclip = s.getClip();
		swidth=sclip.getBounds().width;
		sheight=sclip.getBounds().height;
	}
	
	
	public GScreen() {
		// TODO Auto-generated constructor stub
	}


	/**
	 * Allow the screen to be set later to allow the draw to occur, but not needed
	 * for the collection to be set up.
	 * 
	 * @param s
	 */
	public void setScreen(Graphics2D s)
	{
		
		screen=s;
		Shape sclip = s.getClip();
		swidth=sclip.getBounds().width;
		sheight=sclip.getBounds().height;
		
	}
	
	/**
	 * Return the current internal screen object.  
	 * @return
	 */
	public Graphics2D getScreen()
	{
		return screen;
		
	}
	
	
	// consider returning some kind of object identifier for easier manipulation
	public void add(int x, int y, GObject o)
	{
		if (sprites==null) 
		{
			sprites=new ArrayList<Gsprite>();
		}
		sprites.add(new Gsprite(x,y,o));
	}
	
	public void remove (GObject o)
	{
		sprites.remove(o);
	}
	
	
	public void draw()
	{
		if (screen==null) return;
		
		// Clear the screen
		screen.setColor(Color.WHITE);
		screen.fillRect(0, 0, swidth, sheight);
		
		if (sprites==null) return;
		
		int maxEle = sprites.size();
		if (maxEle==0) return;
	
	    // Iterate over the collection (Java 1.5)
		for (Gsprite sp : sprites)
			sp.draw();
		
		/*for (int i=0; i < maxEle; i++ )
		{
			b=sprites.get(i);
			b.draw(screen);
		}*/
	}
	
}
